package com.longshine.tools.util;

import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DBUtil {
	private static Method getMethod(Class c,String methodName) throws Exception{
		Method[] ms = c.getMethods();
		for(Method m:ms){
			if(m.getName().equals(methodName)){
				return m;
			}
		}
		return null;
	}
	public static List resutSet2Obj(ResultSet set,Class c) throws Exception{
		int cnt = set.getMetaData().getColumnCount();
		List l = new ArrayList();
		while(set.next()){
			Object ins = c.newInstance();
			for(int i = 0;i<cnt;i++){
				String colName = set.getMetaData().getColumnName(i+1);
				Object obj = set.getObject(colName);
				Method m = getMethod(c,StrTmpUtil.dB2JavaString("set_"+colName));
				if(m==null){
					System.out.println("set_"+colName);
					continue;
				}
				Class type = m.getParameterTypes()[0];
				if(type == int.class){
					m.invoke(ins, Integer.parseInt(obj.toString()));
				}
				else if(type == String.class){
					m.invoke(ins, obj == null?null:obj.toString());
				}
				else if(type == float.class){
					m.invoke(ins, Float.parseFloat(obj.toString()));
				}
			}
			l.add(ins);
		}
		return l;
	}
}
